package com.touchyourheart.web.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

  private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

  @ExceptionHandler({SQLException.class, DataAccessException.class})
  public String handleSQLException(HttpServletRequest request, Exception ex) {
    logger.info("SQLException Occured:: URL=" + request.getRequestURL());
    return "databaseError";
  }

  @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "page not found")
  public String handleIOException() {
    logger.error("IOException handler executed");
    // returning 404 error code
    return "404";
  }
  
  @ExceptionHandler(Exception.class)
  public String handleOtherException(Exception exception) {
    logger.error(exception.getMessage(), exception);
    return "error";
  }
}
